//开始缓冲分析
function mhgis_buffer(type) {
  if (type === '点') {
    tb.activate(esri.toolbars.Draw.POINT);
    map.hideZoomSlider();
  } else if (type === '直线') {
    tb.activate(esri.toolbars.Draw.LINE);
    map.hideZoomSlider();
  } else if (type === '曲线') {
    tb.activate(esri.toolbars.Draw.FREEHAND_POLYLINE);
    map.hideZoomSlider();
  } else if (type === '面') {
    tb.activate(esri.toolbars.Draw.POLYGON);
    map.hideZoomSlider();
  } else if (type === '自由面') {
    tb.activate(esri.toolbars.Draw.FREEHAND_POLYGON);
    map.hideZoomSlider();
  }
};

// 画图
function doDraw(geometry) {
  // 根据图形的类型定义显示样式
  switch (geometry.type) {
    case "point":
      var symbol = new esri.symbol.SimpleMarkerSymbol(
        esri.symbol.SimpleMarkerSymbol.STYLE_SQUARE, 10,
        new esri.symbol.SimpleLineSymbol(
          esri.symbol.SimpleLineSymbol.STYLE_SOLID,
          new dojo.Color([255, 0, 0]), 1), new dojo.Color([0,
          255, 0, 0.25
        ]));
      break;
    case "polyline":
      var symbol = new esri.symbol.SimpleLineSymbol(
        esri.symbol.SimpleLineSymbol.STYLE_DASH, new dojo.Color([
          255, 0, 0
        ]), 1);
      break;
    case "polygon":
      var symbol = new esri.symbol.SimpleFillSymbol(
        esri.symbol.SimpleFillSymbol.STYLE_SOLID,
        new esri.symbol.SimpleLineSymbol(
          esri.symbol.SimpleLineSymbol.STYLE_SOLID,
          new dojo.Color([0, 0, 0]), 1), new dojo.Color([255,
          0, 0, 0.25
        ]));
      break;
  }
  // 把绘制的图形添加到map.graphics进行显示
  var graphic = new esri.Graphic(geometry, symbol);
  map.graphics.add(graphic);

  // 如果是面需要先进行simplify操作，否则直接进行buffer
  if (geometry.type === "polygon") {
    geometryService.simplify([graphic], doSimplify);
  } else {
    doBuffer([graphic]);
  }
}

// simplify结束调用buffer
function doSimplify(graphics) {
  doBuffer(graphics);
}

function doBuffer(graphics) {
  // buffer参数
  var params = new esri.tasks.BufferParameters();
  // buffer的范围值，从输入框中获取
  //params.distances = [dojo.byId("distance").value];
  params.distances = 1;
  // 空间参考
  params.bufferSpatialReference = new esri.SpatialReference({
    //wkid : dojo.byId("wkid").value
    wkid: 102100
  });
  // 输出结果的空间参考
  params.outSpatialReference = map.spatialReference;
  params.features = graphics;
  // buffer的单位，从列表框获取
  //params.unit = eval("esri.tasks.BufferParameters." + dojo.byId("unit").value);
  params.unit = esri.tasks.GeometryService.UNIT_KILOMETER;
  // buffer操作
  geometryService.buffer(params, showBuffer);
  console.info('q');
}

// 显示buffer的结果
function showBuffer(features) {
  console.info('1');
  var symbol = new esri.symbol.SimpleFillSymbol(
    esri.symbol.SimpleFillSymbol.STYLE_SOLID,
    new esri.symbol.SimpleLineSymbol(
      esri.symbol.SimpleLineSymbol.STYLE_SOLID, new dojo.Color([
        255, 0, 0, 0.65
      ]), 2), new dojo.Color([255, 0, 0,
      0.35
    ]));
  for (var j = 0; j < features.length; j++) {
    var graphic = new esri.Graphic(features[j].geometry, symbol);
    map.graphics.add(graphic);
  }
  tb.deactivate();
  map.showZoomSlider();

}